package com.jumeng.server.impl;

import com.jumeng.entity.OrderEntity;
import com.jumeng.enums.OrderStatus;
import com.jumeng.mapper.OrderMapper;
import com.jumeng.server.PayOrderService;
import com.jumeng.util.H5WeChatPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Value("${wx.Appid}")
    private String appId;

    @Value("${wx.machId}")
    private String machId;

    @Value("${wx.key}")
    private String key;

    @Value("${wx.callback}")
    private String callback;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map<String, String> h5WeChatPay(String orderNo, String money, String body, String openId) {
        Map<String, String> map = H5WeChatPayUtil.h5WeChatPay(appId, machId, key, orderNo, money, body, callback, openId);
        //保存订单
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderNo(orderNo);
        orderEntity.setCreatedBy(openId);
        orderEntity.setStatus(OrderStatus.WAIT_PRICE);
        orderEntity.setOrderPrice(BigDecimal.valueOf(Integer.parseInt(money)));
        orderEntity.setCreatedTime(new Date());
        orderEntity.setUpdatedTime(new Date());
        orderEntity.setAccountStationNo(openId);
        orderEntity.setCode(UUID.randomUUID().toString());
        orderMapper.insert(orderEntity);
        return map;
    }
}
